package com.yuyou.activity.group.service.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yuyou.activity.comm.service.pay.ZyCreateOrderNotSucc;
import com.yuyou.activity.comm.service.pay.ZyOrderAttach;
import com.yuyou.activity.comm.service.pay.service.IZyPayService;
import com.yuyou.activity.comm.service.tool.MailUtil;
import com.yuyou.activity.comm.service.vo.req.ReqCreateOrderNotice;
import com.yuyou.activity.comm.service.vo.req.ReqRefundOrderNotice;
import com.yuyou.activity.group.service.ActivityGroupSPConfig;
import com.yuyou.activity.group.service.entity.ActivityGroupbuy;
import com.yuyou.activity.group.service.entity.ActivityGroupbuyOrder;
import com.yuyou.activity.group.service.service.IActivityGroupbuyOrderService;
import com.yuyou.activity.group.service.service.IActivityGroupbuyService;
import com.yuyou.activity.group.service.service.IActivitySPNoticeService;
import com.yuyou.activity.util.bean.ResBoolSimpleInfo;
import com.yuyou.activity.util.enums.ActivityGroupBuyEnums;
import com.yuyou.activity.util.enums.ActivityGroupBuyOrderEnums;

@Service
public class ActivitySPNoticeServiceImpl implements IActivitySPNoticeService{

	
	@Autowired
	private IZyPayService zyPayService;
	
	@Autowired
	private IActivityGroupbuyOrderService actGroupbuyOrdService;
	
	@Autowired
	private IActivityGroupbuyService actGroupbuyService;
	
	@Transactional(rollbackFor = Exception.class)
	@Override
	public ResBoolSimpleInfo<String> createOrderNotice(ReqCreateOrderNotice reqCreateOrderNotice) {
		//记录流水
		ResBoolSimpleInfo<String> res=zyPayService.createOrderNotice(reqCreateOrderNotice);
		if(res.isSuccess()){			
			String attach=reqCreateOrderNotice.getAttach();
			ZyOrderAttach attachObj=JSONObject.parseObject(attach, new TypeReference<ZyOrderAttach>() {});			
			ActivityGroupbuyOrder order=actGroupbuyOrdService.selectByOrderId(attachObj.getProjectCode(), reqCreateOrderNotice.getOrderId());
			ActivityGroupbuyOrder update=new ActivityGroupbuyOrder();
			update.setActivityGroupbuyOrderId(order.getActivityGroupbuyOrderId());
			update.setPayStatus(ActivityGroupBuyOrderEnums.payStatus.SUCCESS.getValue());
			actGroupbuyOrdService.updateByPrimaryKeySelective(update);
			
			//活动状态
			boolean booSta=false;
			ActivityGroupbuy activityGroupbuy=actGroupbuyService.selectByPrimaryKey(order.getActivityGroupbuyId());
			if(activityGroupbuy.getGroupType().intValue()==ActivityGroupBuyEnums.groupType.SINGLE.getValue()){
				booSta=true;
			}else{
				//判断该活动是否满人
				List<ActivityGroupbuyOrder> succOrderList=actGroupbuyOrdService.findSuccByGroupId(order.getActivityGroupbuyId());
				if(succOrderList.size()>=ActivityGroupSPConfig.getConfig(order.getProjectCode()).getNum()){
					booSta=true;
				}
			}			
			if(booSta){
				ActivityGroupbuy upActivityGroupbuy=new ActivityGroupbuy();
				upActivityGroupbuy.setActivityGroupbuyId(order.getActivityGroupbuyId());
				upActivityGroupbuy.setStatus(ActivityGroupBuyEnums.status.TRUE_SUCCESS.getValue());
				actGroupbuyService.updateByPrimaryKeySelective(upActivityGroupbuy);
			}				
		}
		return res;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public ResBoolSimpleInfo<String> refundNotice(
			ReqRefundOrderNotice reqRefundOrderNotice) {
		ResBoolSimpleInfo<String> res=new ResBoolSimpleInfo<String>();
		//记录流水
		ResBoolSimpleInfo<Integer> resNotic=zyPayService.refundOrderNotice(reqRefundOrderNotice);
		if(resNotic.isSuccess()){
			String attach=reqRefundOrderNotice.getAttach();
			ZyOrderAttach attachObj=JSONObject.parseObject(attach, new TypeReference<ZyOrderAttach>() {});			
			ActivityGroupbuyOrder order=actGroupbuyOrdService.selectByRefundId(attachObj.getProjectCode(), reqRefundOrderNotice.getRefundId());
			ActivityGroupbuyOrder update=new ActivityGroupbuyOrder();
			update.setActivityGroupbuyOrderId(order.getActivityGroupbuyOrderId());
			update.setRefundStatus(resNotic.getData());
			actGroupbuyOrdService.updateByPrimaryKeySelective(update);
			
			res.setSuccess(true);
			res.setData(ZyCreateOrderNotSucc.getRes());
		}else{
			res.setSuccess(false);
		}
		
		return res;
	}


}
